<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2017/1/6
 * Time: 17:34
 */

namespace app\pc\helper;


class PcFunctionHelper {

    /**
     *取图片地址
     */
    public static function getImgUrl($id,$size=false){
        $PICTURE_URL = config('picture_url');
        $head_url = $PICTURE_URL.$id;
        if($size) $head_url = $head_url.'&size='.$size;
        return $head_url;
    }

    /**
     * 取用户头像地址
     */
    public static function getHeadUrl($head,$size=false){
        $PICTURE_URL = config('picture_url');
        if(empty($head)){
            $head_url = config('static_url').'/img/common/xlogo.png';
        }else{
            if(intval($head)>0){
                $head_url = $PICTURE_URL.$head;
                if($size) $head_url = $head_url.'&size='.$size;
            }else{
                $head_url = $head;
            }
        }
        return $head_url;
    }

    /**
     * 生成分页
     */
    public static function paging($now=1,$size=10,$num,$url,$params=[]) {
        $params = array_merge(input('get.'), $params);
        if ($num < 1) return '';
        //计算总页数
        $pageNum = ceil($num / $size);
        $lis     = '';
        if ($now != 1) {
            //上一页按钮
            $params['p'] = $now - 1;
            $curl        = url($url, $params);
            $lis .= '<li class="item prev"><a href="' . $curl . '"><span>上一页</span></a></li>';
        } else {
            //上一页按钮禁止
            $lis .= '<li class="item prev prev-disabled"><span>上一页</span></li>';
        }
        if ($now <= 5) {
            for ($i = 1; $i <= 7 && $i <= $pageNum; $i++) {
                if ($now == $i) {
                    $lis .= '<li class="item active">' . $i . '</li>';
                } else {
                    $params['p'] = $i;
                    $curl        = url($url, $params);
                    $lis .= '<li class="item"><a class="num" href="' . $curl . '">' . $i . '</a></li>';
                }
            }
            if ($pageNum > 7) {
                $lis .= '<li class="item dot">...</li>';
            }
        }
        if ($now > 5) {
            $params['p'] = 1;
            $curl_p1     = url($url, $params);
            $params['p'] = 2;
            $curl_p2     = url($url, $params);
            $lis .= '<li class="item"><a class="num" href="' . $curl_p1 . '">1</a></li>
                    <li class="item"><a class="num" href="' . $curl_p2 . '">2</a></li>
                    <li class="item dot">...</li>';
            for ($i = $now - 2; $i <= $now + 2 && $i <= $pageNum; $i++) {
                if ($now == $i) {
                    $lis .= '<li class="item active">' . $i . '</li>';
                } else {
                    $params['p'] = $i;
                    $curl        = url($url, $params);
                    $lis .= '<li class="item"><a class="num" href="' . $curl . '">' . $i . '</a></li>';
                }
            }
            if ($pageNum > $now + 2) {
                $lis .= '<li class="item dot">...</li>';

            }
        }
        if ($pageNum > $now) {
            //下一页按钮
            $params['p'] = $now + 1;
            $curl        = url($url, $params);
            $lis .= '<li class="item next"><a href="' . $curl . '"><span>下一页</span></a></li>';
        } else {
            //下一页按钮禁止
            $lis .= '<li class="item next next-disabled"><span>下一页</span></li>';
        }
        $pageHtml = '<div class="m-page">
                        <div class="inner">
                            <ul class="items">' . $lis . '</ul>
                        </div>
                     </div>';
        return $pageHtml;
    }
}
